package com.eunion.core.common.request;

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.eunion.core.common.request.impl.PaginationRequestImpl;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, defaultImpl = PaginationRequestImpl.class)
public interface PaginationRequest<T> extends SortingRequest<T> {

    Integer getPageNo();

    Integer getPageSize();

    default Pageable toPageable() {
        Sort sort = toSort();
        if (sort == null) {
            return new PageRequest(getPageNo() - 1, getPageSize());
        }
        return new PageRequest(getPageNo() - 1, getPageSize(), sort);
    }

}
